package com.lagou.edu.controller;

import com.lagou.edu.service.LoginService;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/sss")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @GetMapping
    public String index() {
        return "login";
    }

    @PostMapping("/login")
    public ModelAndView login(String username, String password, HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView();

        Boolean result = loginService.login(username, password);
        if (result) {
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            modelAndView.setViewName("list");

        } else {
            modelAndView.addObject("message", "登录失败");
            modelAndView.setViewName("login");
        }

        return modelAndView;
    }
}
